package wang.unclecat.mylibrary;

import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.resultset.ResultSet;
import ohos.mp.metadata.binding.MetaDataFramework;
import ohos.mp.metadata.binding.common.Log;
import ohos.mp.metadata.binding.dao.inner.dataability.DataAbilityDao;
import ohos.mp.metadata.binding.metadata.DataAbilityMetaData;
import ohos.mp.metadata.binding.metadata.MetaData;
import ohos.mp.metadata.binding.schema.SchemaParseUtil;
import ohos.mp.metadata.binding.schema.SchemaProto;
import ohos.utils.net.Uri;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class DataAbilityDaoExtra extends DataAbilityDao {
    private DataAbilityHelper helper;

    private ArrayList<MetaData> cacheList;
    private ArrayList<MetaData> addedCache;
    private Object CACHE_LOCK;

    public DataAbilityDaoExtra() {
        super();
        helper = (DataAbilityHelper) getSuperField("helper");
        CACHE_LOCK = getSuperField("CACHE_LOCK");
        cacheList = (ArrayList<MetaData>) getSuperField("cacheList");
        addedCache = (ArrayList<MetaData>) getSuperField("addedCache");
    }

    @Override
    public List<MetaData> queryMetaData(boolean b) {
        synchronized (this.CACHE_LOCK) {
            ArrayList var3 = this.queryMetaDataInner();
            this.cacheList.clear();
            this.cacheList.addAll(var3);
            return var3;
        }
    }

    private ArrayList<MetaData> queryMetaDataInner() {
        ArrayList var1 = new ArrayList();
        SchemaProto var2 = SchemaParseUtil.findSchema(this.requestItem.metaId);

        ResultSet var3;
        try {
            String[] var4;
            if (this.requestItem.properties != null && !this.requestItem.properties.isEmpty()) {
                var4 = this.requestItem.properties.toArray(new String[1]);
            } else {
                var4 = var2.getFields();
            }

            DataAbilityPredicates var5 = null;
            if (this.requestItem.predicates != null) {
                var5 = this.requestItem.predicates.getDataAbilityPredicate();
            }

            var3 = this.helper.query(Uri.parse(this.requestItem.dataUri), var4, var5);
        } catch (DataAbilityRemoteException var6) {
            return var1;
        }

        if (var3 == null) {
            Log.warn("DataAbilityDao queried nothing from datasource: " + this.requestItem.dataUri);
            return var1;
        } else {
            while (var3.goToNextRow()) {
                MetaData var7 = MetaDataFramework.createMetaData(this.requestItem);
                if (var7 instanceof DataAbilityMetaData) {
                    ((DataAbilityMetaData) var7).mapColumns(var3);
                }

                var7.addInnerObserver(this);
                var1.add(var7);
            }

            return var1;
        }
    }


    private Object getSuperField(String fieldName) {
        try {
            Class<?> clazz = Class.forName("ohos.mp.metadata.binding.dao.inner.dataability.DataAbilityDao");
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
            //调用方法
            return field.get(this);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
